import {clickingUtils} from './clickingUtils.js' ;

export class clickingTools
{
    constructor(page, scrapingTools, tabHandlingTools, highlightsTools)
    {
        this.page = page;
        this.scrapingTools = scrapingTools;
        this.tabHandlingTools = tabHandlingTools ;
        this.highlightsTools = highlightsTools ;
        this.utils = clickingUtils ;
    }

    async handleTabsAndClick({x,y})
    {
        const newPagePromise = this.tabHandlingTools.catchNewPage() ;
        await this.page.ref.waitForTimeout(200) ;
        await this.page.ref.mouse.click(x, y,{delay:50});
        this.tabHandlingTools.handleTabClose();
        this.tabHandlingTools.handleTabOpen(newPagePromise);
    }

    async clearInputFieldWhenFocused()
    {
        for(let i=0;i<50;i++) await this.page.ref.keyboard.press('ArrowRight') ;
        for(let i=0;i<50;i++) await this.page.ref.keyboard.press('Backspace') ;
        for(let i=0;i<10;i++) await this.page.ref.keyboard.press('ArrowLeft') ;
    }

    async insertInputText(x,y,inputText)
    {   
        await this.clearInputFieldWhenFocused()
        let inputArray = await inputText.split('\n') ;

        for(let i = 0; i < inputArray.length; i++)
        {
            await this.page.ref.keyboard.type(inputArray[i]);
            if(i != inputArray.length-1) await this.page.ref.keyboard.press('Shift+Enter');
        }
        await this.page.ref.keyboard.press('Enter');
        return `Clicked at postion ${x} ${y} and typed ${inputText} and then sumitted by clicking Enter`;
    }


    async selectOption(index, inputText)
    {
        const element = this.scrapingTools.getScrapedData(index) ;
        let selector = `select` ;
        for(const key of this.utils.getIdentifyElementSelector())
        {
            if(element[key])
            {
                const attr = key === 'ariaLabel' ? 'aria-label' : key;
                selector += `[${attr}="${element[key]}"]`;
            } 
        } 
        const elementLocator = await this.page.ref.locator(selector) ;
        const cnt = await elementLocator.count() ;
        if(cnt != 1) return 'failed to find one select element' ; ///might not work
        await elementLocator.first().selectOption({label:inputText}) ;
        return `successfully selected option ${inputText}` ;
    }


    async click({index, inputText})
    {
        await this.highlightsTools.highlightElements([]) ;

        const element = this.scrapingTools.getScrapedData(index) ;
        const tag = element.tag ;

        console.error(element) ;
        
        if(tag == 'select')
        {
            const resp = await this.selectOption(index, inputText) ;
            if(resp[0]=='s') return resp;
        }

        let {boundingBox:{x,y,width,height}} = element ;
        x+=width*0.5 ;
        y+=height*0.5 ;

        await this.handleTabsAndClick({x,y});

        if(inputText == '') return `Clicked at postion ${x} ${y} `;
        
        
        let role = '' ;
        if(element.role) role = element.role;

        if(tag != 'input' && tag!='select' && tag !='button' && role != 'combobox' && tag != 'textarea' && role != 'textbox') return `F` 
        return await this.insertInputText(x,y,inputText);
    }
}